package com.tanhua.dubbo.server.api;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.enums.CommentType;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.pojo.Video;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class ManageApiImpl implements ManageApi {


    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 评论列表翻页
     *
     * @param page      当前页
     * @param pagesize  每页显示条数
     * @param messageID 消息ID
     * @param sortProp  排序字段
     * @param sortOrder ascending 升序 descending 降序
     * @return
     */
    @Override
    public List<Comment> queryCommentList(Integer page, Integer pagesize, String messageID, String sortProp, String sortOrder) {
        //创建集合存储数据
        List<Comment> commentList = new ArrayList<>();
        if (sortProp.equals("createDate")){
            sortOrder = "created";
        }
        //如果排序字段和排序规则不为空,按照条件查询
        if (StrUtil.isNotEmpty(sortProp) && StrUtil.isNotEmpty(sortOrder)){
            //按照字段,升序查询
            if (sortOrder.equals("ascending")) {
                PageRequest pageRequest = PageRequest.of(page - 1, pagesize,Sort.by(Sort.Order.asc(sortProp)));
                Query query = Query.query(Criteria.where("publishId").is(new ObjectId(messageID))
                                .and("commentType").is(CommentType.COMMENT.getType()))
                        .with(pageRequest);
                  commentList = mongoTemplate.find(query, Comment.class);
            } else {
                //按照字段,降序查询
                if (sortOrder.equals("descending")) {
                    PageRequest pageRequest = PageRequest.of(page - 1, pagesize, Sort.by(Sort.Order.desc(sortProp)));
                    Query query = Query.query(Criteria.where("publishId").is(new ObjectId(messageID))
                            .and("commentType").is(CommentType.COMMENT.getType())).with(pageRequest);
                   commentList = mongoTemplate.find(query, Comment.class);
                }
            }
            }else {
                //不进行排序查询
            PageRequest pageRequest = PageRequest.of(page - 1, pagesize);
            Query query=Query.query(Criteria.where("publishId").is(new ObjectId(messageID))
                    .and("commentType").is(CommentType.COMMENT.getType())).with(pageRequest);
            commentList = mongoTemplate.find(query, Comment.class);
        }

        //返回数据
        return commentList;
    }

    /**
     * 视频记录翻页
     * @param page 当前页
     * @param pagesize 每页显示条数
     * @param uid 当前发布视频的用户id
     * @param sortProp 排序字段
     * @param sortOrder ascending 升序 descending 降序
     * @return
     */
    @Override
    public List<Video> queryVideosList(Integer page, Integer pagesize, Long uid, String sortProp, String sortOrder) {
        //创建集合存储数据
        List<Video> commentList = new ArrayList<>();
        if (sortProp.equals("createDate")){
            sortOrder = "created";
        }
        if ("createDate".equals(sortProp)){
            sortProp="created";
        }
        //如果排序字段和排序规则不为空,按照条件查询
        if (StrUtil.isNotEmpty(sortProp) && StrUtil.isNotEmpty(sortOrder)) {
            //按照字段,升序查询
            if (sortOrder.equals("ascending")) {
                PageRequest pageRequest = PageRequest.of(page - 1, pagesize, Sort.by(Sort.Order.asc(sortProp)));
                Query query = Query.query(Criteria.where("userId").is(uid)).with(pageRequest);
                commentList = mongoTemplate.find(query, Video.class);
            } else {
                //按照字段,降序查询
                if (sortOrder.equals("descending")) {
                    PageRequest pageRequest = PageRequest.of(page - 1, pagesize, Sort.by(Sort.Order.desc(sortProp)));
                    Query query = Query.query(Criteria.where("userId").is(uid)).with(pageRequest);
                    commentList = mongoTemplate.find(query, Video.class);
                }
            }
        }else{
            //不进行排序查询
            PageRequest pageRequest = PageRequest.of(page - 1, pagesize);
            Query query = Query.query(Criteria.where("userId").is(uid)).with(pageRequest);
            commentList = mongoTemplate.find(query, Video.class);
        }

        //返回数据
        return commentList;
    }

}